import { MODEL_VALUE_KEY } from '@/components/modal/emit'

export default function useModal() {
  const visible = ref(false)
  const modalData = ref({})

  function open(data) {
    if (data) modalData.value = data
    visible.value = true
  }

  function close(emit) {
    updateModelValue({ emit })
    visible.value = false
  }

  function updateModelValue({
    emit,
    key = MODEL_VALUE_KEY,
    value = false,
    isModal = true,
  }) {
    emit(`update:${key}`, value)
    isModal && emit('close')
  }

  return {
    visible,
    modalData,
    open,
    close,
  }
}
